package org.locker.device.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.locker.common.core.domain.R;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.common.log.annotation.Log;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.satoken.utils.LoginHelper;
import org.locker.common.web.core.BaseController;
import org.locker.domain.Order;
import org.locker.domain.bo.OrderBo;
import org.locker.domain.enums.WithdrawTypeEnum;
import org.locker.domain.vo.OrderVo;
import org.locker.service.IOrderService;
import org.locker.service.IWithdrawService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/order")
public class OrderController extends BaseController {

    private final IOrderService orderService;
    private final IWithdrawService withdrawService;

    /**
     * 查询订单列表
     */
    @SaCheckPermission("device:order:list")
    @GetMapping("/list")
    public R<Page<OrderVo>> list(OrderBo bo, PageQuery pageQuery) {
        return R.ok(orderService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("device:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OrderBo bo, HttpServletResponse response) {
        List<OrderVo> list = orderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", OrderVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param orderId 主键
     */
    @SaCheckPermission("device:order:query")
    @GetMapping("/{orderId}")
    public R<OrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long orderId) {
        return R.ok(orderService.queryById(orderId));
    }

    /**
     * 提现
     * @param orderNo
     * @param userId
     * @return
     */
    @GetMapping("/withdraw")
    public R<Long> withdraw(@RequestParam("orderNo") String orderNo,@RequestParam("userId") Long userId) {
        return R.ok(withdrawService.withdrawBySystem(userId,orderNo, WithdrawTypeEnum.SYSTEM.getCode()));
    }

    /**
     * 结束订单
     * @param orderNo
     * @return
     */
    @GetMapping("/close")
    public R<OrderVo> close(@RequestParam("orderNo") String orderNo,@RequestParam("isNeedOpen") Boolean isNeedOpen) {
        //用户+订单号查询,防止被刷
        Order order = orderService.getOne(Wrappers.<Order>lambdaQuery()
            .eq(Order::getOrderNo, orderNo));
        order.setRemark("后台系统结束订单!");
        orderService.close(order, isNeedOpen,false);
        return R.ok();
    }
}
